import store from '../store';
import jssdkUtil from '@/utils/jssdkUtil';

const TokenKey = 'vue_app_template_token';

/**
 * 检查当前用户的有没有对应的授权
 */
export function check(authority) {
  const current = [store.getters.userCurrentAuthority];
  return current.some(item => authority.includes(item));
}

export async function getToken() {
  return new Promise(async(resolve, reject) => {
    try {
      const res = await jssdkUtil.getStore(TokenKey);
      console.log(res, '获取store中的token');
      resolve(res);
    } catch (error) {
      console.error('error', error);
      reject(error);
    }
  });
}

export async function setToken(token) {
  return new Promise(async(resolve, reject) => {
    try {
      const res = await jssdkUtil.setStore(TokenKey, token);
      console.log(res, '设置store中的token');
      resolve(res.resultCode);
    } catch (error) {
      console.error('error', error);
      reject(error);
    }
  });
}

export function removeToken() {
  return new Promise(async(resolve, reject) => {
    try {
      const res = await jssdkUtil.deleteStore(TokenKey);
      console.log(res, '删除store中的token');
      resolve(res.resultCode);
    } catch (error) {
      console.error('error', error);
      reject(error);
    }
  });
}

export default {
  check,
  getToken,
  setToken,
  removeToken,
};
